var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderItemInfo: [],  //订单列表页参数
    submitCommCount:0 //已经提交的评论数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取上页（订单列表）数据
    var orderItemInfo = wx.getStorageSync("orderItemInfo");
    // console.log(orderItemInfo);
    for (var i = 0; i < orderItemInfo.length; i++){
      orderItemInfo[i].images = [];
      orderItemInfo[i].content = "";
      orderItemInfo[i].score = 5;
      orderItemInfo[i].isAnonymous = 1;
      orderItemInfo[i].evaluate = 0;
    }
    this.setData({
      orderItemInfo: orderItemInfo
    })
    console.log(orderItemInfo)
  },
  
  /**
   * 发表评论
   */
  submitComm: function (e) {
    var orderItemInfo = this.data.orderItemInfo;
    var allFill = true;
    for (var i = 0; i < orderItemInfo.length; i++) {
      var cont = orderItemInfo[i].content.trim();
      if(cont==""){
        wx.showToast({
          title: '评价不能为空',
          icon:"none"
        })
        allFill = false;
        break;
      }
    }
    if (allFill){
      for (var i = 0; i < orderItemInfo.length; i++) {
        wx.showLoading();
        var pics = '';
        orderItemInfo[i].images.forEach(function(item){
          pics += item.path + ',';
        });
        if(pics!=''){
          pics = pics.substring(0,pics.length-1)
        }
        // 发布评论
        var params = {
          url: "/p/prodComm",
          method: "POST",
          data: {
            content: orderItemInfo[i].content,
            score: orderItemInfo[i].score,
            evaluate: orderItemInfo[i].evaluate,
            isAnonymous: orderItemInfo[i].isAnonymous,
            orderItemId: orderItemInfo[i].orderItemId,
            prodId: orderItemInfo[i].prodId,
            pics: pics
          },
          callBack:  (res) => {
            this.setData({
              submitCommCount: this.data.submitCommCount+1
            });
            if (this.data.submitCommCount == orderItemInfo.length) {
              wx.showModal({
                title: '',
                content: '评价成功，感谢您！',
                showCancel:false,
                success(res) {
                  wx.navigateBack();
                }
              })
            }
            wx.hideLoading();
          }
        };
        http.request(params);
      }

      
    }
  },


  /**
   * 上传图片
   */
  getUploadImg: function(e) {
    const idx = e.target.dataset.idx
    console.log(idx);
    var ths = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({  
          mask: true
        })
        var params = {
          url: "/p/file/upload",
          filePath: tempFilePaths[0],
          name: 'file',
          callBack: function (res2) {
            wx.hideLoading();
            var img = {};
            img.path = JSON.parse(res2).filePath;
            img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
            var orderItemInfo = ths.data.orderItemInfo;
            orderItemInfo[idx].images.push(img);
            ths.setData({
              orderItemInfo: orderItemInfo
            })
          }
        };
        http.upload(params);
      }
    })
  },

  /**
   * 删除图片
   */
  removeImage(e) {
    const idx = e.target.dataset.idx
    const index = e.target.dataset.index
    var orderItemInfo = this.data.orderItemInfo;
    orderItemInfo[index].images.splice(idx, 1)
    this.setData({
      orderItemInfo: orderItemInfo
    });
  },

  onContentInput:function(e){
    const index = e.target.dataset.index
    var orderItemInfo = this.data.orderItemInfo;
    orderItemInfo[index].content = e.detail.value;
    this.setData({
      orderItemInfo: orderItemInfo
    });
  },


  /**
  * 匿名评价
  * 每一项的选择事件
  */
  onSelectedItem: function (e) {
    var index = e.target.dataset.index;// 获取data- 传进来的index
    var orderItemInfo = this.data.orderItemInfo;// 获取评论项
    var isAnonymous = orderItemInfo[index].isAnonymous; // 获取当前评价的选中状态
    if (isAnonymous==1){
      isAnonymous = 0;
    }else{
      isAnonymous = 1;
    }
    orderItemInfo[index].isAnonymous = isAnonymous; // 改变状态
    this.setData({
      orderItemInfo: orderItemInfo
    });
  },

  onStarChange:function(e){
    var index = e.detail.idx;
    var val = e.detail.val;
    var evaluate = 0;
    var orderItemInfo = this.data.orderItemInfo;
    if(val<3){
      evaluate = 2;
    }else if(val==3){
      evaluate = 1;
    }
    orderItemInfo[index].score = val;
    orderItemInfo[index].evaluate = evaluate;
    this.setData({
      orderItemInfo: orderItemInfo
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }



})